package com.zmj.security;

import com.zmj.security.model.UserSubject;

import javax.servlet.http.HttpServletRequest;
import java.util.function.Function;

/**
 * @Author: zmj
 * @Date: 2025/1/14
 */
public class UsernamePasswordAuthenticator implements Authenticator {

    private String loginPath;
    private Function<HttpServletRequest, UserSubject> authenticator;

    public UsernamePasswordAuthenticator() {
    }

    public UsernamePasswordAuthenticator(String loginPath, Function<HttpServletRequest, UserSubject> authenticator) {
        this.loginPath = loginPath;
        this.authenticator = authenticator;
    }

    public String getLoginPath() {
        return loginPath;
    }

    public void setLoginPath(String loginPath) {
        this.loginPath = loginPath;
    }

    public Function<HttpServletRequest, UserSubject> getAuthenticator() {
        return authenticator;
    }

    public void setAuthenticator(Function<HttpServletRequest, UserSubject> authenticator) {
        this.authenticator = authenticator;
    }

    @Override
    public boolean support(HttpServletRequest request) {
        return request.getRequestURI().equals(loginPath);
    }

    @Override
    public UserSubject authenticate(HttpServletRequest request) {
        return authenticator.apply(request);
    }

}
